package com.itheima.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.PictureSortMapper;
import com.itheima.pojo.PictureSort;
import com.itheima.service.PictureSortService;
import com.itheima.utils.IdWorker;
import com.itheima.pojo.PictureSortVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class PictureSortServiceImpl implements PictureSortService {

    @Autowired
    private PictureSortMapper pictureSortMapper;

    /**
     * 添加分类
     * @param pictureSortVO
     */
    @Override
    public void add(PictureSortVO pictureSortVO) {
        PictureSort pictureSort = JSON.parseObject(JSON.toJSONString(pictureSortVO), PictureSort.class);
        //生成id
        IdWorker idWorker=new IdWorker();
        String id = idWorker.nextId()+"";
        pictureSort.setUid(id);
        pictureSortMapper.insert(pictureSort);
    }

    /**
     * 删除分类
     * @param pictureSortVO
     */
    @Override
    public void delete(PictureSortVO pictureSortVO) {
        Example example = createExample(pictureSortVO);
        pictureSortMapper.deleteByExample(example);
    }

    /**
     * 编辑分类
     * @param pictureSortVO
     */
    @Override
    public void edit(PictureSortVO pictureSortVO) {
        PictureSort pictureSort = JSON.parseObject(JSON.toJSONString(pictureSortVO), PictureSort.class);
        //根据uid查询需要编辑的对象
        Example example = new Example(PictureSort.class);
        Example.Criteria criteria = example.createCriteria();
        if(pictureSortVO!=null) {
            if (pictureSortVO.getUid() != null && !"".equals(pictureSortVO.getUid())) {
                criteria.andEqualTo("uid", pictureSortVO.getUid());
            }
        }
        pictureSortMapper.updateByExample(pictureSort,example);
    }

    /**
     * 获取图片分类列表
     * @param pictureSortVO
     * @return
     */
    @Override
    public PageInfo<PictureSort> getList(PictureSortVO pictureSortVO) {
        Integer currentPage = pictureSortVO.getCurrentPage();
        Integer pageSize = pictureSortVO.getPageSize();
        PageHelper.startPage(currentPage,pageSize);
        Example example = createExample(pictureSortVO);
        example.setOrderByClause("sort DESC");
        List<PictureSort> pictureSorts = pictureSortMapper.selectByExample(example);
        return new PageInfo<>(pictureSorts);
    }

    /**
     * 通过Uid获取分类
     * @param pictureSortVO
     * @return
     */
    @Override
    public PictureSort getPictureSortByUid(PictureSortVO pictureSortVO) {
        String uid = pictureSortVO.getUid();
        return pictureSortMapper.selectByPrimaryKey(uid);
    }

    /**
     * 置顶分类
     * @param pictureSortVO
     */
    @Override
    public void stick(PictureSortVO pictureSortVO) {
        String uid = pictureSortVO.getUid();
        //获取数据库中排序字段最大值
        int max = pictureSortMapper.getMax();
        //最大值+1然后存入数据库,达到指定操作
        pictureSortMapper.stick(uid,max+1);
    }

    private Example createExample(PictureSortVO pictureSortVO) {
        Example example=new Example(PictureSort.class);
        Example.Criteria criteria = example.createCriteria();
        if(pictureSortVO!=null) {
            //分类图片id
            if (pictureSortVO.getFileUid() != null && !"".equals(pictureSortVO.getFileUid())) {
                criteria.andEqualTo("fileUid", pictureSortVO.getFileUid());
            }
            // 是否显示
            if (pictureSortVO.getIsShow() != null && !"".equals(pictureSortVO.getIsShow())) {
                criteria.andEqualTo("isShow", pictureSortVO.getIsShow());
            }
            // 分类名
            if (pictureSortVO.getKeyword() != null && !"".equals(pictureSortVO.getKeyword())) {
                criteria.andLike("name", "%"+pictureSortVO.getKeyword()+"%");
            }
            // 播客uid
            if (pictureSortVO.getParentUid() != null && !"".equals(pictureSortVO.getParentUid())) {
                criteria.andEqualTo("parentUid", pictureSortVO.getParentUid());
            }
            // 状态
            if (pictureSortVO.getStatus() != null && !"".equals(pictureSortVO.getStatus())) {
                criteria.andEqualTo("status", pictureSortVO.getStatus());
            }
            // 排序
            if (pictureSortVO.getSort() != null && !"".equals(pictureSortVO.getSort())) {
                criteria.andEqualTo("sort", pictureSortVO.getSort());
            }
            // 主键
            if (pictureSortVO.getUid() != null && !"".equals(pictureSortVO.getUid())) {
                criteria.andEqualTo("uid", pictureSortVO.getUid());
            }
        }
        return example;
    }
}
